<?php
require_once APPLICATION_PATH.'/models/Admin.php';

class LoginController extends Zend_Controller_Action {
    public function init(){
        session_start();
    }
    public function indexAction(){

    }

    public function loginAction(){
        $tbladmin = new Admin();
        $username = $this->_getParam('username');
        $password = $this->_getParam('password');

        $userinfo = $tbladmin->check($username, $password);
        if($userinfo != null){
            $_SESSION['username'] = $userinfo['uname'];
            $_SESSION['lastlogin'] = $userinfo['lastlogin'];

            //更新登录时间
            $set = array (
                'lastlogin' => time(),
            );
            $where = ("uname='".$_SESSION['username']."'");
            $tbladmin->update($set, $where);

            header('Location:'.ADMIN_PATH);
            exit;
        }

        echo '<script>alert("用户名或密码错误！");</script>';
        $this->render('index');
    }

    public function logoutAction(){
        unset($_SESSION['username']);
        unset($_SESSION['lastlogin']);
        $this->render('index');
    }

}
